package org.s63.kaptcha;

import com.google.code.kaptcha.Producer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;

@Controller
public class KaptchaController {
    @Autowired
    private Producer producer;

    @GetMapping("/kaptcha")
    public void code(HttpServletResponse response, HttpSession session) throws Exception {
        String code = producer.createText();
        session.setAttribute("code", code);
        System.out.println("生成的验证码：" + code);
        BufferedImage image = producer.createImage(code);
        response.setContentType("image/png");
        ImageIO.write(image, "png", response.getOutputStream());
    }
}
